package com.bdqn.t330.ch07.servlet;

import com.bdqn.t330.ch07.entity.SysUser;
import com.bdqn.t330.ch07.service.UserService;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/ListServlet")
public class ListServlet extends HttpServlet {


    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("doget.....");
        //获得servlet上下文对象
        ServletContext context = getServletContext();
        //初始化spring容器
        //记得导入spring-webmvc jar
        //注意需要在web.xml中配置好监听 listener,加载spring配置文件
        WebApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(context);
        UserService userService = (UserService) ctx.getBean("userServiceImp");
        SysUser sysUser = new SysUser();
        sysUser.setUserName("t");
        sysUser.setUserRole(1);
        List<SysUser> list = userService.getUser(sysUser);
        req.setAttribute("list", list);
        req.getRequestDispatcher("index.jsp").forward(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        super.doPost(req, resp);
    }
}
